<?php

namespace QF;

class Config {

    static public $table = 'config';

    public function getAll() {
        $result = Db()->getAll(static::$table)->result();
        return $result;
    }

    public function getkeyValue() {
        $rows = $this->getAll();
        $configs = array();
        foreach ($rows as $row) {
            $configs[$row['name']] = $row['value'];
        }
        return $configs;
    }

    /**
     * 配置缓存10秒
     */
    public function getValue($name) {
        $value = cache('config', $name);
        if (!$value) {
            $where = array('name' => $name);
            $value = Db()->getOne(static::$table)->fields('value')->where($where)->result();
            cache('config', $name, $value, 10);
        }
        return $value;
    }

    public function setOne($name, $value) {
        $where = array('name' => $name);
        $arrSets = array('value' => $value);
        $result = Db()->update(static::$table, $arrSets, $where);

        if (!$result) {
            return false;
        }

        $result = cache('config', $name, $value, 10);

        if (!$result) {
            return false;
        }

        return true;
    }
}
